package com.hut.oa.controller;

import java.net.UnknownHostException;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hut.oa.model.bean.Apply;
import com.hut.oa.model.bean.ApplyLeave;
import com.hut.oa.model.bean.ApplyObject;
import com.hut.oa.model.bean.Dept;
import com.hut.oa.model.bean.DeptOffice;
import com.hut.oa.model.bean.User;
import com.hut.oa.model.dao.apply.ApplyDao;
import com.hut.oa.model.dao.apply.ApplyRecordDao;
import com.hut.oa.model.dao.user.DeptDao;
import com.hut.oa.service.ApplyService;

/**
 * 审批管理
 * @author 朱嘉茵
 *
 */
@Controller
@RequestMapping("/apply")
public class ApplyController {
	
	Logger log=LoggerFactory.getLogger(getClass());
	
	@Autowired
	ApplyDao ad;
	
	@Autowired
	ApplyService as;
	
	@Autowired
	ApplyRecordDao ard;
	
	@Autowired
	DeptDao dd;
	
	/**
	 * 查看申请详情
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping("/show")
	public String toShowApply(Model model,@RequestParam("id")Long id){
		Apply apply=ad.findById(id);
		model.addAttribute("apply", apply);
		model.addAttribute("record", ard.findByApplyOrderByTime(apply));
		return "apply/showApply";
	}
	
	/**
	 * 到申请列表页面
	 * @param model
	 * @param pageable
	 * @param session[
	 * @return
	 */
	@RequestMapping("/submit")
	public String toApply(@RequestParam(value="state",required=false)Integer state,Model model,@PageableDefault(value=10,sort={"time"},direction=Direction.DESC)Pageable pageable,HttpSession session){
		User user=(User) session.getAttribute("userinfo");
		Page<Apply> applys=null;
		if(state==null){
			applys=ad.findByUserId(user.getId(), pageable);
		}else{
			applys=ad.findByStatusAndUser(state,user, pageable);
		}
		model.addAttribute("applys", applys);
		return "apply/apply";
	}
	
	/**
	 * 删除单条申请
	 * @param id
	 */
	@RequestMapping("/submit/delete")
	@ResponseBody
	public void deleteApply(@RequestParam("id")Long id){
		ad.delete(id);
		log.info("删除申请信息：id= {}",id);
	}
	
	/**
	 * 获取单条物品申请数据到编辑页面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("/submit/getone")
	public String getDept(@RequestParam("id")Long id,Model model){
		Apply apply=ad.findById(id);
		//判断是哪种类型
		if(apply.getApplyObject()!=null){
			model.addAttribute("applyObject", apply.getApplyObject());
			return "apply/editApply_object";
		}else if(apply.getApplyLeave()!=null){
			model.addAttribute("applyLeave", apply.getApplyLeave());
			return "apply/editApply_leave";
		}else{
			return "apply/apply";
		}
	}
	
	/**
	 * 选择类型页面
	 * @return
	 */
	@RequestMapping("/submit/choosetype")
	public String chooseType(){
		return "apply/editApply_type";
	}
	
	/**
	 * 到添加申请页面
	 * @param num
	 * @return
	 */
	@RequestMapping("/submit/add")
	public String toEditForm(@RequestParam("num")int num){
		//model.addAttribute("depts", dd.findAllByOrderByGrade());
		switch (num) {
		case 1:
			return "apply/editApply_object";
		case 2:
			return "";
		case 3:
			return "";
		case 4:
			return "apply/editApply_business";
		case 5:
			return "apply/editApply_leave";
		case 6:
			return "";
		default:
			return "apply/editApply_object";
		}
	}
	
	/**
	 * 保存物品申请表单
	 * @param applyObject
	 */
	@RequestMapping(value="/submit/save/object",method=RequestMethod.POST)
	@ResponseBody
	public void saveObjectForm(ApplyObject applyObject,HttpSession session){
		User user=(User) session.getAttribute("userinfo");
		log.info("保存物品申请： {}",applyObject);
		as.saveObject(applyObject,user);
	}
	
	/**
	 * 保存请假申请表单
	 * @param applyObject
	 */
	@RequestMapping(value="/submit/save/leave",method=RequestMethod.POST)
	@ResponseBody
	public void saveLeaveForm(ApplyLeave applyLeave,HttpSession session){
		User user=(User) session.getAttribute("userinfo");
		log.info("保存请假申请： {}",applyLeave);
		as.saveLeave(applyLeave, user);
	}
	
	/**
	 * 审批管理列表
	 * @param model
	 * @param session
	 * @return
	 */
	@RequestMapping("/approval")
	public String toApproval(@RequestParam(value="state",required=false)Integer state,Model model,HttpSession session,@PageableDefault(value=10,sort={"time"},direction=Direction.DESC)Pageable pageable){
		User user=(User) session.getAttribute("userinfo");
		Page<Apply> applys=null;
		if(state==null){
			applys=ad.findByDeptOffice(user.getDeptOffice(),pageable);
		}else{
			applys=ad.findByStatusAndDeptOffice(state,user.getDeptOffice(), pageable);
		}
		model.addAttribute("applys", applys);
		return "apply/approval";
	}
	
	/**
	 * 审核申请
	 * @param value
	 */
	@RequestMapping("/approval/check")
	@ResponseBody
	public void checkApply(@RequestParam("office")DeptOffice office,@RequestParam("approval")int approval,@RequestParam("applyId")Long applyId,@RequestParam("remark")String remark,HttpSession session){
		User user=(User) session.getAttribute("userinfo");
		try {
			as.checkApply(office,approval, applyId,remark, user);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
	}
}
